﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using weixin.Models;

namespace weixin.Services
{
    public class DomainService
    {
        private readonly weixin.Models.WeixinOpenPlatEntities db = new Models.WeixinOpenPlatEntities();

        public List<tb_Domains> Query()
        {
            return db.tb_Domains.ToList();
        }

        public tb_Domains GetById(int id)
        {
            return db.tb_Domains.FirstOrDefault(r => r.id == id);
        }

        public tb_Domains GetByHost(string host)
        {
            return db.tb_Domains.FirstOrDefault(r => r.host == host);
        }

        public tb_Domains GetByState(string state)
        {
            return db.tb_Domains.FirstOrDefault(r => r.state == state);
        }

        public bool Delete(string ids)
        {
            string[] _ids = ids.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in _ids)
            {
                int _id = int.Parse(item);
                Models.tb_Domains model = db.tb_Domains.First(r => r.id == _id);
                db.tb_Domains.Remove(model);
            }
            return db.SaveChanges() > 0;
        }

        public bool AddOrUpdate(tb_Domains model)
        {
            if (model.id > 0)
            {
                db.Entry<tb_Domains>(model).State = System.Data.EntityState.Modified;
            }
            else
            {
                db.Entry<tb_Domains>(model).State = System.Data.EntityState.Added;
            }
            return db.SaveChanges() > 0;
        }
    }
}